﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessComponents;
using BusinessComponents.Managers;

namespace WaterfrontLodge.WebForms.RoomMgt
{
    public partial class RoomEdit : System.Web.UI.Page
    {
        #region Declaration
        IRoomMgr roomMgr = ManagerFactory.RoomMgr;
        IRoomTypeMgr typeMgr = ManagerFactory.RoomTypeMgr;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IEnumerable<RoomType> typeList = typeMgr.FindAllRoomTypes();
                ddlRoomType.DataSource = typeList;
                ddlRoomType.DataTextField = "Description";
                ddlRoomType.DataValueField = "TypeCode";
                ddlRoomType.DataBind();

                string roomNo;
                roomNo = Request.QueryString["RoomNo"];
                if (roomNo!=null)
                {
                    RetrieveData(roomNo);
                }
                
            }

        }

        public void RetrieveData(string roomNo)
        {
            using (WaterFrontEntities cntx = new WaterFrontEntities())
            {
                var room = roomMgr.GetRoom(roomNo);
                if (room != null)
                {
                    txtRoomNo.Text = room.RoomNo;
                    for (int i = 0; i < ddlRoomType.Items.Count; i++)
                    {
                        if (ddlRoomType.Items[i].Value == room.TypeCode.ToString())
                        {
                            ddlRoomType.Items[i].Selected = true;
                        }
                    }
                    for (int i = 0; i < rdbStatus.Items.Count; i++)
                    {
                        if (rdbStatus.Items[i].Value == room.Status.ToString())
                        {
                            rdbStatus.Items[i].Selected = true;
                        }
                    }
                    txtRemarks.Text = room.Remarks;
                    txtCreate.Text = string.Format("{0:dd MMM, yyyy HH:mm:ss}", room.CreatedDateTime);
                }
                else
                {
                    lblMessage.Text = "The room can not be found!";
                }

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var room = roomMgr.GetRoom(txtRoomNo.Text);
            if (room != null)
            {
                //room.RoomNo = txtRoomNo.Text;
                room.TypeCode = Convert.ToInt16(ddlRoomType.SelectedItem.Value);
                room.Remarks = txtRemarks.Text;
                room.Status = Convert.ToInt16(rdbStatus.SelectedItem.Value);
                //room.CreatedDateTime = Convert.ToDateTime(txtCreate.Text);
                roomMgr.Save();
                lblMessage.Text = "Room information updated successfully!";
                Response.Redirect("~/WebForms/Room/RoomView.aspx");
            }
        }

        protected void ddlRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblMessage.Text = ddlRoomType.SelectedItem.Value;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/WebForms/Room/RoomView.aspx");
        }
    }
}